/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginDialog.java
 *
 * Created on Nov 18, 2011, 12:16:34 AM
 */
package Login;

import DBKoneksi.TestConection;
import java.sql.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Piyo
 */
public class LoginDialog extends javax.swing.JDialog {

    /** Creates new form LoginDialog */
    public LoginDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        UserNameLabel = new javax.swing.JLabel();
        PasswordLabel = new javax.swing.JLabel();
        UserNameField = new javax.swing.JTextField();
        PasswordField = new javax.swing.JPasswordField();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jDesktopPane1.setBackground(new java.awt.Color(255, 255, 255));

        UserNameLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
        UserNameLabel.setText("UserName");
        UserNameLabel.setBounds(50, 60, 100, 30);
        jDesktopPane1.add(UserNameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        PasswordLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
        PasswordLabel.setText("Password");
        PasswordLabel.setBounds(50, 100, 80, 30);
        jDesktopPane1.add(PasswordLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        UserNameField.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
        UserNameField.setBounds(140, 60, 190, 27);
        jDesktopPane1.add(UserNameField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        PasswordField.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
        PasswordField.setBounds(140, 100, 190, 27);
        jDesktopPane1.add(PasswordField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        OKButton.setText("OK");
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });
        OKButton.setBounds(80, 140, 60, 30);
        jDesktopPane1.add(OKButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        CancelButton.setText("Cancel");
        CancelButton.setBounds(230, 140, 80, 30);
        jDesktopPane1.add(CancelButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("Georgia", 1, 14));
        jLabel2.setText("Masukkan Username dan Password Anda");
        jLabel2.setBounds(40, 10, 310, 30);
        jDesktopPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Login/q.jpg"))); // NOI18N
        jLabel1.setBounds(0, 0, 380, 190);
        jDesktopPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-388)/2, (screenSize.height-227)/2, 388, 227);
    }// </editor-fold>//GEN-END:initComponents

private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        String UserName =UserNameField.getText();
        String password =PasswordField.getText();
         TestConection connec = new TestConection();
        Login loginbaru=new Login();
        loginbaru.setUsername(UserName);
        loginbaru.setPassword(password);
    try{
        
            String query="Select * from LOGIN where username='"+UserName
                    +
                    "' and password='" + password+ "'";
            Statement statement = connec.getConnection().createStatement();
            ResultSet result = statement.executeQuery(query);

            if (result.next()){
            JOptionPane.showMessageDialog(this, "Selamat Datang\n"+loginbaru.getUsername());   
            FormUtama utama=new FormUtama();
            utama.setVisible(true);
            this.dispose();
            }
            else {
             JOptionPane.showMessageDialog(this,"Username dan Password yang anda masukkan salah");   
            }
            statement.close();
        } catch(Exception ex){
           System.out.println("message : "+ex.getMessage());
        }

}//GEN-LAST:event_OKButtonActionPerformed

    /**
     * @param args the command line arguments
     */

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton OKButton;
    private javax.swing.JPasswordField PasswordField;
    private javax.swing.JLabel PasswordLabel;
    private javax.swing.JTextField UserNameField;
    private javax.swing.JLabel UserNameLabel;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
}
